//
//  LayerAnimalView.m
//  UILbrary
//
//  Created by mac on 16/8/16.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "LayerAnimalView.h"

#define DEGREES_TO_RADIANS(degrees) (degrees) / 180.0 * M_PI
#define RGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]
#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a/255.0]
#define CGRGB(r,g,b) RGB(r,g,b).CGColor
#define iCGRGB(r,g,b) (id)CGRGB(r,g,b)
#define CGRGBA(r,g,b,a) RGBA(r,g,b,a).CGColor
#define iCGRGBA(r,g,b,a) (id)CGRGBA(r,g,b,a)
@interface LayerAnimalView ()
{
    UIImage *background;
    CGPoint center;
    CGRect fullRect;
}
@end


@implementation LayerAnimalView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.


- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor lightGrayColor];
        [self initDrawingRects];
        
    }
    
    return self;
}

- (void)initDrawingRects
{
    center = CGPointMake(0.5, 0.5);
    fullRect = CGRectMake(0.0, 0.0, 1.0, 1.0);
    
//    _innerRimBorderWidth = _showInnerRim?_innerRimBorderWidth:0.0;
//    _innerRimWidth = _showInnerRim?_innerRimWidth:0.0;
    
//    innerRimRect = fullRect;
//    innerRimBorderRect = CGRectMake(innerRimRect.origin.x + _innerRimBorderWidth,
//                                    innerRimRect.origin.y + _innerRimBorderWidth,
//                                    innerRimRect.size.width - 2 * _innerRimBorderWidth,
//                                    innerRimRect.size.height - 2 * _innerRimBorderWidth);
//    faceRect = CGRectMake(innerRimRect.origin.x + _innerRimWidth,
//                          innerRimRect.origin.y + _innerRimWidth,
//                          innerRimRect.size.width - 2 * _innerRimWidth,
//                          innerRimRect.size.height - 2 * _innerRimWidth);
//    rangeLabelsRect = CGRectMake(faceRect.origin.x + (_showRangeLabels ? _rangeLabelsWidth : 0.0),
//                                 faceRect.origin.y + (_showRangeLabels ? _rangeLabelsWidth : 0.0),
//                                 faceRect.size.width - 2 * (_showRangeLabels ? _rangeLabelsWidth : 0.0),
//                                 faceRect.size.height - 2 * (_showRangeLabels ? _rangeLabelsWidth : 0.0));
//    scaleRect = CGRectMake(rangeLabelsRect.origin.x + _scalePosition,
//                           rangeLabelsRect.origin.y + _scalePosition,
//                           rangeLabelsRect.size.width - 2 * _scalePosition,
//                           rangeLabelsRect.size.height - 2 * _scalePosition);
}


- (void)drawRect:(CGRect)rect {

    if (background == nil)
    {
        UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextScaleCTM(context, rect.size.width , rect.size.height);
//        [self drawGauge:context];
        background = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    
    [background drawInRect:rect];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextScaleCTM(context, rect.size.width , rect.size.height);
    
    [self drawNeedle:context];
     
}

- (void)drawNeedle:(CGContextRef)context
{
//    [self rotateContext:context fromCenter:center withAngle:DEGREES_TO_RADIANS(180 + _scaleStartAngle + (currentValue - _minValue) / (_maxValue - _minValue) * (_scaleEndAngle - _scaleStartAngle))];
    
    int _needleStyle = 0;
    CGFloat _needleHeight = 0.34;
    CGFloat _needleWidth = 0.035;
    switch (_needleStyle)
    {
        case 0:
        {
            CGContextSetShadow(context, CGSizeMake(0.05, 0.05), 8.0);
            
            // Left Needle
            UIBezierPath *leftNeedlePath = [UIBezierPath bezierPath];
            [leftNeedlePath moveToPoint:center];
            [leftNeedlePath addLineToPoint:CGPointMake(center.x - _needleWidth, center.y)];
            [leftNeedlePath addLineToPoint:CGPointMake(center.x, center.y - _needleHeight)];
            [leftNeedlePath closePath];
            [RGB(176, 10, 19) setFill];
            [leftNeedlePath fill];
            
            // Right Needle
            UIBezierPath *rightNeedlePath = [UIBezierPath bezierPath];
            [rightNeedlePath moveToPoint:center];
            [rightNeedlePath addLineToPoint:CGPointMake(center.x + _needleWidth, center.y)];
            [rightNeedlePath addLineToPoint:CGPointMake(center.x, center.x - _needleHeight)];
            [rightNeedlePath closePath];
            [RGB(252, 18, 30) setFill];
            [rightNeedlePath fill];
        }
            break;
            
        case 1:
        {
            UIBezierPath *needlePath = [UIBezierPath bezierPath];
            [needlePath moveToPoint:CGPointMake(center.x - _needleWidth, center.y)];
            [needlePath addLineToPoint:CGPointMake(center.x + _needleWidth, center.y)];
            [needlePath addLineToPoint:CGPointMake(center.x, center.x - _needleHeight)];
            [needlePath closePath];
            
#define SHADOW_OFFSET  0.008
            CGContextTranslateCTM(context, -SHADOW_OFFSET, -SHADOW_OFFSET);
            [RGBA(0, 0, 0, 40) setFill];
            [RGBA(0, 0, 0, 20) setStroke];
            [needlePath fill];
            needlePath.lineWidth = 0.004;
            [needlePath stroke];
            CGContextTranslateCTM(context, SHADOW_OFFSET, SHADOW_OFFSET);
            
            [RGB(255, 104, 97) setFill];
            [RGB(255, 104, 97) setStroke];
            [needlePath fill];
            needlePath.lineWidth = 0.004;
            [needlePath stroke];
        }
            break;
            
        default:
            break;
    }
    
//    [self drawNeedleScrew:context];
}



@end
